package com.dashboard.reading.controller;

import com.User.entity.User;
import com.dashboard.reading.entity.Book;
import com.dashboard.reading.service.BookService;
import com.utils.APIRespones;
import com.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("api/book")
public class BookController {
    @Autowired
    private BookService bookService;


    @GetMapping("/list")
    public APIRespones getBookInfo(
            @RequestParam(value = "token") String token,
            @RequestParam(value = "keywords", required = false) String keywords,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
            @RequestParam(value = "sortField", required = false, defaultValue = "id") String sortField,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "asc") String sortOrder) {

        Integer userId = Utils.getTokenUserId(token);
        if (userId == null) {
            return APIRespones.failure("Invalid token.");
        }

        List<Book> books = bookService.getBookInfo(userId, keywords, page, pageSize, sortField, sortOrder);
        Map<String, Object> data = new HashMap<>();
        data.put("books", books);
        System.out.println(books);
        return APIRespones.success(data);
    }
    @PostMapping("/list")
    public APIRespones addBook(@RequestParam(value = "token") String token, @RequestBody Book book) {
        Integer userId = Utils.getTokenUserId(token);
        if (userId == null) {
            return APIRespones.failure("Invalid token.");
        }

        book.setUserId(userId);
        boolean success = bookService.addBook(book);
        if (success) {
            return APIRespones.success(null);
        } else {
            return APIRespones.failure("Failed to add book.");
        }
    }

    @PutMapping("/list/{bookId}")
    public APIRespones updateBook(@PathVariable("bookId") Integer bookId, @RequestParam(value = "token") String token, @RequestBody Book newBook) {
        Integer userId = Utils.getTokenUserId(token);
        if (userId == null) {
            return APIRespones.failure("Invalid token.");
        }

        Book book = bookService.findBookById(bookId);
        if (book == null) {
            return APIRespones.failure("Book not found.");
        }

        // Update the book with the new values
        book.setTitle(newBook.getTitle());
        book.setTags(newBook.getTags());
        book.setType(newBook.getType());
        book.setStatus(newBook.getStatus());
        book.setDate(newBook.getDate());
        book.setAuthor(newBook.getAuthor());
        book.setPublic(newBook.getPublic());

        book.setUserId(userId);
        boolean success = bookService.updateBook(book);
        if (success) {
            return APIRespones.success(null);
        } else {
            return APIRespones.failure("Failed to update book.");
        }
    }
    @DeleteMapping("/list/{bookId}")
    public APIRespones deleteBook(@PathVariable("bookId") Integer bookId, @RequestParam(value = "token") String token) {
        Integer userId = Utils.getTokenUserId(token);
        if (userId == null) {
            return APIRespones.failure("Invalid token.");
        }

        Book book = bookService.findBookById(bookId);
        if (book == null) {
            return APIRespones.failure("Book not found.");
        }

        if (!book.getUserId().equals(userId)) {
            return APIRespones.failure("You don't have permission to delete this book.");
        }

        boolean success = bookService.deleteBook(bookId);
        if (success) {
            return APIRespones.success(null);
        } else {
            return APIRespones.failure("Failed to delete book.");
        }
    }
    @PostMapping("/image")
    public APIRespones updateBookImage(@RequestParam("file") MultipartFile file, @RequestParam("bookId") Integer bookId){
        return bookService.uploadBookImage(file, bookId);
    }
    @PostMapping("/detail")
    public APIRespones updateBookDetails(@RequestBody  Book book) {
        return bookService.updateBookDetails(book);
    }
}
